package com.lzw.controller.superAdmin;

import com.lzw.service.superAdmin.SuperLoginService;
import com.lzw.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@CrossOrigin(origins = "http://localhost:7070")
@RestController
@RequestMapping("/super")
public class SuperLogin {
    @Autowired
    private SuperLoginService superLoginService;

    @PostMapping
    public Map<String, Object> login(@RequestBody Map<String, String> loginForm) {
        String username = loginForm.get("username");
        String password = loginForm.get("password");

        Map<String, Object> response = new HashMap<>();

        Map<String, Object> admin = superLoginService.getAdmin(username,password);

        if (admin != null) {
            // 创建JWT令牌zzz
            String token = JwtUtils.generateJwt(admin);
            response.put("success", true);
            response.put("message", "登录成功");
            response.put("token", token);
            response.put("admin",admin);
        } else {
            response.put("success", false);
            response.put("message", "用户名或密码错误");
        }
        return response;
    }

    @GetMapping("/info")
    public Map<String, Object> getAdmin(@RequestHeader("Authorization") String token) {
        Map<String, Object> response = new HashMap<>();
        try {
            // 验证JWT令牌
            Claims claims = JwtUtils.parseJWT(token.replace("Bearer ", ""));
            Integer adminId = (Integer) claims.get("admin_id");

            Map<String, Object> admin = superLoginService.getAdminById(adminId);
            response.put("success", true);
            response.put("data", admin);
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "无效的令牌");
        }
        return response;
    }

    @PutMapping("/update")
    public Map<String, Object> updateAdmin(@RequestHeader("Authorization") String token,
                                           @RequestBody Map<String, String> updateData) {
        Map<String, Object> response = new HashMap<>();
        try {
            Claims claims = JwtUtils.parseJWT(token.replace("Bearer ", ""));
            Integer adminId = (Integer) claims.get("admin_id");

            // 解析前端传来的字段
            String name = updateData.get("name");
            String phone = updateData.get("phone");
            String email = updateData.get("email");

            boolean updated = superLoginService.updateAdmin(adminId, name, phone, email);

            if (updated) {
                response.put("success", true);
                response.put("message", "信息更新成功");
            } else {
                response.put("success", false);
                response.put("message", "更新失败");
            }
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "无效的令牌");
        }
        return response;
    }
}
